var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), a = require("../../../@babel/runtime/helpers/asyncToGenerator"), e = getApp(), o = null, i = null, s = "", n = 1;

Page({
    data: {
        Data: {},
        time: "",
        Day: "00",
        Hour: "00",
        Min: "00",
        Sec: "00",
        stop: !1,
        show: !1,
        loadingImg: !0,
        ithumb: "",
        canvasWidth: "",
        canvasHeight: "",
        star_time: "1582963135",
        end: "",
        timer: null,
        info: {},
        isSetNotice: !1,
        postData: {},
        room_id: "",
        shareBol: !1,
        loadingMessage: "海报生成中",
        code_id: ""
    },
    onLoad: function(t) {
        var a = this;
        if (this.pollStautsTimer && clearTimeout(this.pollStautsTimer), this.getiPnoneBottom(), 
        t.scene) {
            var r = decodeURIComponent(t.scene);
            if (r) {
                var l = [];
                l = r.split(",");
                for (var d = 0; d < l.length; d++) {
                    var c;
                    "id" == (c = l[d].split("="))[0] ? this.setData({
                        room_id: c[1]
                    }) : "share" == c[0] ? this.setData({
                        shareBol: !0
                    }) : "mid" == c[0] && e._setMid(c[1]);
                }
            }
        } else t.cd && this.setData({
            code_id: t.cd
        }), t.mid && e._setMid(t.mid), t.id && this.setData({
            room_id: t.id
        }), t.share && (this.data.shareBol = !0);
        wx.getSystemInfo({
            success: function(t) {
                n = wx.getSystemInfoSync().pixelRatio, s = 2 * t.windowWidth < 750 ? t.windowWidth / 620 : t.windowWidth / 750, 
                o = 620 * n * s, i = 883 * n * s, a.setData({
                    canvasWidth: parseInt(o),
                    canvasHeight: parseInt(i)
                });
            }
        }), this.getPoster(), this.getData();
    },
    getiPnoneBottom: function() {
        try {
            wx.getStorageSync("iPhoneBottom") && this.setData({
                iPnoneBottomBol: !0
            });
        } catch (t) {}
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {
        clearTimeout(this.data.timer), this.pollStautsTimer && clearTimeout(this.pollStautsTimer);
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        return t && (a = t), {
            title: "欢迎进入" + this.data.postData.nickname + "的" + this.data.info.nickname + "直播间！",
            query: "id=" + this.data.room_id + "&share=1&mid=" + a
        };
    },
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), a = "";
        return t && (a = t), {
            title: "欢迎进入" + this.data.postData.nickname + "的" + this.data.info.nickname + "直播间！",
            path: "/packageD/directSeeding/foreshow/foreshow?id=" + this.data.room_id + "&share=1&mid=" + a
        };
    },
    showCloseBtn: function() {
        this.setData({
            show: !1
        });
    },
    posterBtn: function() {
        this.setData({
            show: !0
        });
    },
    initData: function() {
        this.setData({
            time: "",
            Day: "",
            Hour: "",
            Min: "",
            Sec: ""
        });
    },
    checkStart: function() {
        var t = e.getNetAddresss("plugin.room.frontend.live-list.room-notice");
        e._postNetWork({
            url: t,
            showToastIn: !1,
            data: {
                id: this.data.room_id
            },
            success: function(t) {
                var a = t.data;
                1 == a.result && that.data.shareBol && 3 == a.data.status && wx.showModal({
                    title: "提示",
                    content: "主播正在直播中，跳转直播间",
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.redirectTo({
                            url: "/packageD/directSeeding/livePage/livePage?id=" + that.data.room_id + "&playerType=live&cd=" + that.data.code_id
                        });
                    }
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    countdown: function() {
        var t = this, a = Date.parse(new Date()) / 1e3;
        if (this.data.end - a <= 0) return this.setData({
            stop: !0
        }), void (this.data.code_id && this.checkStart());
        var e = 0;
        e = a >= this.data.star_time ? this.data.end - a : this.data.star_time - a;
        var o = parseInt(e / 60 / 60 / 24), i = parseInt(e / 60 / 60 % 24), s = parseInt(e / 60 % 60), n = parseInt(e % 60);
        i = i > 9 ? i : "0" + i, s = s > 9 ? s : "0" + s, n = n > 9 ? n : "0" + n, this.setData({
            Day: o,
            Hour: i,
            Min: s,
            Sec: n
        }), this.data.timer = setTimeout(function() {
            t.countdown();
        }, 1e3);
    },
    getAvaterInfo: function() {
        var t = 2 == this.data.postData.status ? "预告" : 3 == this.data.postData.status ? "直播中 " : "回放", a = wx.createCanvasContext("myCanvas");
        a.fillStyle = "#FFFFFF", a.fillRect(0, 0, this.data.canvasWidth, this.data.canvasHeight);
        var e = 56 * n * s, o = e, i = e, r = 215 * n * s, l = 23 * n * s;
        a.save(), a.beginPath(), a.arc(o / 2 + r, i / 2 + l, o / 2, 0, 2 * Math.PI, !1), 
        a.clip(), this.data.postData.shop_logo && a.drawImage(this.data.postData.shop_logo, r, l, o, i), 
        a.restore(), a.setFontSize(13 * n), a.setFillStyle("#000"), a.fillText(this.data.postData.shop_name, 296 * n * s, 60 * n * s), 
        this.drawImg(a, 560 * n * s, 440 * n * s, this.data.postData.cover, this.data.postData.imgData.width, this.data.postData.imgData.height, 30 * n * s, 98 * n * s);
        var d = 437 * n * s, c = 117 * n * s, h = 127 * n * s, u = 42 * n * s, g = 10 * n;
        a.save(), a.translate(d, c), this.drawRoundRectPath(a, h, u, g), a.fillStyle = "#ff7200", 
        a.fill(), a.restore(), a.setFillStyle("#fff"), a.setFontSize(14 * n), 2 == t.length ? a.fillText(t, 474 * n * s, 148 * n * s) : a.fillText(t, 460 * n * s, 148 * n * s);
        var f = 42 * n * s, m = f, p = f, v = 52 * n * s, w = 117 * n * s;
        a.save(), a.beginPath(), a.arc(m / 2 + v, p / 2 + w, m / 2, 0, 2 * Math.PI, !1), 
        a.clip(), a.drawImage(this.data.postData.avatar, v, w, m, p), a.restore();
        var D = this.data.postData.nickname.split(""), T = "", S = [];
        a.setFontSize(28 * n * s), a.setFillStyle("#fff");
        for (var x = 0; x < D.length; x++) a.measureText(T).width < 160 * n ? T += D[x] : (x--, 
        S.push(T), T = "");
        if (S.push(T), S.length > 2) {
            for (var _ = S.slice(0, 2), I = _[1], P = "", y = [], F = 0; F < I.length && a.measureText(P).width < 306 * n * s; F++) P += I[F];
            y.push(P);
            var M = y[0] + "...";
            _.splice(1, 1, M), S = _;
        }
        for (var k = 0; k < S.length; k++) a.fillText(S[k], 108 * n * s, 146 * n * s + k * (28 * n * s));
        a.restore(), a.save();
        var W = this.data.postData.title.split(""), B = "", H = [];
        a.setFontSize(28 * n * s), a.setFillStyle("#333333");
        for (var N = 0; N < W.length; N++) a.measureText(B).width < 160 * n ? B += W[N] : (N--, 
        H.push(B), B = "");
        if (H.push(B), H.length > 2) {
            for (var b = H.slice(0, 2), A = b[1], R = "", C = [], q = 0; q < A.length && a.measureText(R).width < 306 * n * s; q++) R += A[q];
            C.push(R);
            var z = C[0] + "...";
            b.splice(1, 1, z), H = b;
        }
        for (var L = 0; L < H.length; L++) a.fillText(H[L], 41 * n * s, 620 * n * s + L * (28 * n * s));
        a.restore(), a.setFontSize(12 * n), a.setFillStyle("#ff7200"), a.fillText("直播时间 " + this.data.postData.live_time, 36 * n * s, 730 * n * s);
        var U = 48 * n * s, G = U, j = U, E = 35 * n * s, J = 763 * n * s;
        a.save(), a.beginPath(), a.arc(G / 2 + E, j / 2 + J, G / 2, 0, 2 * Math.PI, !1), 
        a.clip(), a.drawImage(this.data.postData.m_avatar, E, J, G, j), a.restore();
        var K = (this.data.postData.m_nickname + "的分享").split(""), O = "", Q = [];
        a.setFontSize(28 * n * s), a.setFillStyle("#666666");
        for (var V = 0; V < K.length; V++) a.measureText(O).width < 160 * n ? O += K[V] : (V--, 
        Q.push(O), O = "");
        if (Q.push(O), Q.length > 2) {
            for (var X = Q.slice(0, 2), Y = X[1], Z = "", $ = [], tt = 0; tt < Y.length && a.measureText(Z).width < 306 * n * s; tt++) Z += Y[tt];
            $.push(Z);
            var at = $[0] + "...";
            X.splice(1, 1, at), Q = X;
        }
        for (var et = 0; et < Q.length; et++) a.fillText(Q[et], 94 * n * s, 796 * n * s + et * (28 * n * s));
        a.restore(), a.save(), a.drawImage(this.data.postData.qr_code, 408 * n * s, 581 * n * s, 180 * n * s, 180 * n * s), 
        a.restore(), a.setFontSize(12 * n), a.setFillStyle("#333"), a.fillText("长按保存图片", 430 * n * s, 800 * n * s), 
        a.draw(!1, this.toimage());
    },
    drawImg: function(t, a, e, o, i, s, n, r) {
        console.log("cWidth", a), console.log("cHeight", e), console.log("imgWidth", i), 
        console.log("imgHeight", s);
        var l = a / i, d = e / s;
        i > a && s > e || i < a && s < e ? l > d ? t.drawImage(o, 0, (s - e / l) / 2, i, e / l, n, r, a, e) : t.drawImage(o, (i - a / d) / 2, 0, a / d, s, n, r, a, e) : i < a ? t.drawImage(o, 0, (s - e / l) / 2, i, e / l, n, r, a, e) : t.drawImage(o, (i - a / d) / 2, 0, a / d, s, n, r, a, e);
    },
    drawRoundRectPath: function(t, a, e, o) {
        t.beginPath(0), t.arc(a - o, e - o, o, 0, Math.PI / 2), t.lineTo(o, e), t.arc(o, e - o, o, Math.PI / 2, Math.PI), 
        t.lineTo(0, o), t.arc(o, o, o, Math.PI, 3 * Math.PI / 2), t.lineTo(a - o, 0), t.arc(a - o, o, o, 3 * Math.PI / 2, 2 * Math.PI), 
        t.lineTo(a, e - o), t.closePath();
    },
    toimage: function() {
        var t = this;
        wx.hideLoading(), this.setData({
            showBtn: !0
        });
        var a = this;
        setTimeout(function() {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: t.data.canvasWidth,
                height: t.data.canvasHeight,
                destWidth: 4 * t.data.canvasWidth,
                destHeight: 4 * t.data.canvasHeight,
                canvasId: "myCanvas",
                success: function(t) {
                    console.log(t);
                    var e = t.tempFilePath;
                    a.setData({
                        ithumb: e,
                        loadingImg: !1
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }, 1500);
    },
    saveShareImg: function() {
        wx.showLoading({
            title: "保存中"
        }), wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: this.data.canvasWidth,
            height: this.data.canvasHeight,
            destWidth: 4 * this.data.canvasWidth,
            destHeight: 4 * this.data.canvasHeight,
            canvasId: "myCanvas",
            success: function(t) {
                console.log(t), wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "图片保存成功",
                            icon: "success",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        wx.hideLoading();
                    }
                });
            }
        });
    },
    getPoster: function() {
        var t = this, a = e.getNetAddresss("plugin.room.frontend.live-list.room-post");
        e._postNetWork({
            url: a,
            showToastIn: !1,
            data: {
                id: this.data.room_id
            },
            success: function(a) {
                var e = a.data;
                1 == e.result ? (t.setData({
                    postData: e.data
                }), t.downImg()) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    downImg: function() {
        var t = this;
        this.data.postData.avatar && wx.downloadFile({
            url: this.data.postData.avatar,
            success: function(a) {
                t.setData({
                    "postData.avatar": a.tempFilePath
                }), t.data.postData.cover && wx.downloadFile({
                    url: t.data.postData.cover,
                    success: function(a) {
                        t.setData({
                            "postData.cover": a.tempFilePath
                        }), wx.getImageInfo({
                            src: t.data.postData.cover,
                            success: function(a) {
                                t.setData({
                                    "postData.imgData": a
                                }), t.data.postData.m_avatar && wx.downloadFile({
                                    url: t.data.postData.m_avatar,
                                    success: function(a) {
                                        t.setData({
                                            "postData.m_avatar": a.tempFilePath
                                        }), t.data.postData.qr_code && wx.downloadFile({
                                            url: t.data.postData.qr_code,
                                            success: function(a) {
                                                t.setData({
                                                    "postData.qr_code": a.tempFilePath
                                                }), t.data.postData.shop_logo ? wx.downloadFile({
                                                    url: t.data.postData.shop_logo,
                                                    success: function(a) {
                                                        t.setData({
                                                            "postData.shop_logo": a.tempFilePath
                                                        }), t.getAvaterInfo();
                                                    },
                                                    fail: function(a) {
                                                        t.setData({
                                                            loadingMessage: "海报生成失败，商城图标下载失败"
                                                        }), console.log(a, "失败");
                                                    }
                                                }) : (t.setData({
                                                    "postData.shop_logo": ""
                                                }), t.getAvaterInfo());
                                            },
                                            fail: function(a) {
                                                console.log(a, "失败"), t.setData({
                                                    loadingMessage: "海报生成失败，小程序二维码下载失败"
                                                });
                                            }
                                        });
                                    },
                                    fail: function(a) {
                                        t.setData({
                                            loadingMessage: "海报生成失败，用户头像图片下载失败"
                                        }), console.log(a, "失败");
                                    }
                                });
                            }
                        });
                    },
                    fail: function(a) {
                        t.setData({
                            loadingMessage: "海报生成失败，直播封面图片下载失败"
                        }), console.log(a, "失败");
                    }
                });
            },
            fail: function(a) {
                t.setData({
                    loadingMessage: "海报生成失败，主播头像图片下载失败"
                }), console.log(a, "失败");
            }
        });
    },
    getData: function() {
        var t = this, a = this, o = e.getNetAddresss("plugin.room.frontend.live-list.room-notice");
        e._postNetWork({
            url: o,
            showToastIn: !1,
            data: {
                id: this.data.room_id
            },
            success: function(e) {
                var o = e.data;
                if (1 == o.result) {
                    var i = o.data.anchor_member_id;
                    if (a.data.shareBol && 3 == o.data.status) return void wx.showModal({
                        title: "提示",
                        content: "主播正在直播中，跳转直播间",
                        showCancel: !1,
                        success: function(t) {
                            t.confirm && wx.redirectTo({
                                url: "/packageD/directSeeding/livePage/livePage?id=" + a.data.room_id + "&playerType=live"
                            });
                        }
                    });
                    if (a.data.shareBol && 4 == o.data.status) return void wx.showModal({
                        title: "提示",
                        content: "直播已经结束,跳转该主播页面",
                        showCancel: !1,
                        success: function(t) {
                            t.confirm && wx.redirectTo({
                                url: "/packageD/directSeeding/anchorDetail/anchorDetail?id=" + i
                            });
                        }
                    });
                    a.setData({
                        info: o.data,
                        end: o.data.live_time,
                        isSetNotice: 1 == o.data.notice
                    }), a.countdown(), t.pollStautsTimer = setInterval(function() {
                        t.pollingLiveStatus();
                    }, 15e3);
                } else wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    },
    pollingLiveStatus: function() {
        var o = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    e._postNetWork({
                        url: e.getNetAddresss("plugin.room.frontend.live-list.room-notice"),
                        data: {
                            id: o.data.room_id
                        },
                        success: function(t) {
                            var a = t.data;
                            1 == a.result && 3 == a.data.status && wx.redirectTo({
                                url: "/packageD/directSeeding/livePage/livePage?id=" + o.data.room_id + "&playerType=live"
                            });
                        },
                        fail: function(t) {
                            console.log(t.msg);
                        }
                    });

                  case 1:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    showToastBtn: function() {
        var t = this, a = null, o = this.data.isSetNotice;
        a = 1 == o ? "plugin.room.frontend.live-list.cancel-notice" : "plugin.room.frontend.live-list.play-notice";
        var i = e.getNetAddresss(a);
        e._postNetWork({
            url: i,
            showToastIn: !1,
            data: {
                id: this.data.room_id
            },
            success: function(a) {
                var e = a.data;
                1 == e.result ? 1 == o ? (t.setData({
                    isSetNotice: 0
                }), wx.showToast({
                    icon: "none",
                    title: "已取消提醒",
                    duration: 1500
                })) : (t.setData({
                    isSetNotice: 1
                }), wx.showToast({
                    icon: "none",
                    title: "已成功订阅",
                    duration: 1500
                })) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t.msg);
            }
        });
    }
});